﻿/*
	为合同提供一个独立的领域服务
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ThirdParty.ESignClient;

namespace SOEI.Solution.StructEntities.Contracts
{
	public class ContractManager : DomainServiceBase, IContractManager
	{

		private readonly IESignManager ESignManager;

		/// <summary>
		/// 【Agreement】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public ContractManager( 
			IESignManager _ESignManager
		)
		{
			ESignManager = _ESignManager;
		}

		#region ---------------------------------------资金监管合同-------------------------------------------------

		/// <summary>
		/// 创建资金监管合同
		/// </summary>
		/// <param name="ContractEntity">合同实体</param>
		/// <returns></returns>
		public async Task CreateRegulatorContract(ContractEntity ContractEntity)
		{
			// 1. 预创建申请
			var FileResult = await this.ESignManager.BeforeCreateFile(ContractEntity.FileName, ContractEntity.FileBuffer);
			// 2. 文件直传
			await this.ESignManager.ExecUpLoad(FileResult.Data, ContractEntity.FileBuffer);
			ContractEntity.FileId = FileResult.Data.FileId;
			// 3. 创建流程 
			var FlowResult = await this.ESignManager.CreateFlowsAsync(ContractEntity.Flow.AccountId, ContractEntity.Flow.OrganiAccountID, ContractEntity.Flow.businessScene);
			ContractEntity.Flow.FlowId = FlowResult.Data.FlowId;
			// 4. 流程添加文档
			var docResult = await this.ESignManager.AddFlowDocAsync(ContractEntity.Flow.FlowId, ContractEntity.FileId, ContractEntity.FileName);
			// 5. 添加自动签署区域(如果有这里需要添加自动签署区域)
			// var autoSignResult = await this.ESignManager.AddPlatformAutoSign(FlowResult.Data.FlowId, FileResult.Data.FileId);
			// 6. 获取盖章区域
			await SetManuals(ContractEntity);
			// 7. 添加手动签署区
			var manualSignResult = await this.ESignManager.AddManualSign(FlowResult.Data.FlowId, FileResult.Data.FileId, ContractEntity.Manuals);
			// 8.开启签署流程
			var startResult = await this.ESignManager.StartFlowAsync(FlowResult.Data.FlowId);
		}

		/// <summary>
		/// 设置查询签署区域
		/// </summary>
		/// <param name="ContractEntity"></param>
		/// <returns></returns>
		protected async Task SetManuals(ContractEntity ContractEntity) 
		{
			if (ContractEntity.Searches.Count == 0) return;
			foreach (var item in ContractEntity.Searches) {
				// 查询结果
				var tokenModel =  await this.ESignManager.SearchWordsPosition(ContractEntity.FileId, item.Keyword);
				foreach (var result in tokenModel.Data) {
					foreach (var itmp in result.PositionList)
					{
						foreach (var itmc in itmp.CoordinateList)
						{
							ContractEntity.Manuals.Add(new ManualEntity
							{
								AccountID = item.AccountID,
								PosPage = itmp.PageIndex, // 印章盖到第几页下面是坐标
								PosX = (decimal.Parse(itmc.PosX) + item.OffsetX).ToString(),
								PosY = (decimal.Parse(itmc.PosY) + item.OffsetY).ToString(),
								ThirdOrderNo = item.ThirdOrderNo, // 签署成功回调参数
								       Order = item.Order
							});
						}
					}
				}
			}
		}

		#endregion



		/*
		 * // 创建之前实体 
			var FileResult = await this.ESignManager.BeforeCreateFile(file.FileName, file);
			// 文件直传
			await this.ESignManager.ExecUpLoad(FileResult.Data, file);
			// 创建流程
			var FlowResult = await this.ESignManager.CreateFlowsAsync(SignetEntity.AccountID, SignetEntity.ORGID, "二手房交易结算资金监管协议");
			// 流程添加文档
			var docResult = await this.ESignManager.AddFlowDocAsync(FlowResult.Data.FlowId, FileResult.Data.FileId, file.FileName);
			// 添加自动签署区域
			var autoSignResult = await this.ESignManager.AddPlatformAutoSign(FlowResult.Data.FlowId, FileResult.Data.FileId);
			// 添加手动签署区
			var manualSignResult = await this.ESignManager.AddManualSign(SignetEntity.AccountID, FlowResult.Data.FlowId, FileResult.Data.FileId);
			// 开启签署流程
			var startResult = await this.ESignManager.StartFlowAsync(FlowResult.Data.FlowId);
		 * 
		 */
	}
}
